package home;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

public class Portfolio {
	
	private Vector<Stock> stocksList;   //Vector of all of stocks at their current price.
	private Vector<Double> numberOfShares;  //Vector of current overall shares
	private Vector<Transaction> allTransactionsList; //Takes in all transactions from Transactions.txt and loads them into this Vector
	private int CustomerID;
	private Vector<Double> sharesThroughDate;  //Vector of shares through a certain date.


	public Portfolio() {
		stocksList = new Vector<Stock>();
		allTransactionsList = new Vector<Transaction>();
		
	}
	
	


	
	/**
	 * 
	 * Loads the instance variables with the correct information.  
	 */
	public void initPortfolio() {
		
		//Reads from stocks file and fills portfolio with all of the available stocks and set their number of shares to zero.
		BufferedReader reader2;
		String singleStock;
		
		try {
			reader2 = new BufferedReader(new FileReader("StockQuotes.txt"));
			singleStock = null;
			stocksList.clear();

			while ((singleStock = reader2.readLine()) != null) {
				Stock tempStock = new Stock();
				tempStock.setStockSymbol((singleStock.split(",")[0]));
				tempStock.setStockPrice((Double.parseDouble(singleStock
						.split(",")[1])));
				
				stocksList.add(tempStock);
			}
			reader2.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		numberOfShares = new Vector<Double>(stocksList.size());
		sharesThroughDate = new Vector<Double>(stocksList.size());
		for(int x = 0; x<=stocksList.size()-1;x++) {
			numberOfShares.add(0.0);
		}
	}
	
	
	/**
	 * Iterates through the transactions list and adds or subtracts shares (based on buy or sell transaction) from the current portfolio.  The end result is a vector
	 * that contains the most up to date number of shares in a selected accounts portfolio
	 */
	public void updatePortfolio() {
		//zeros out numberOfShares method
		numberOfShares.clear();
		for(int x = 0; x<=stocksList.size()-1;x++) {
			numberOfShares.add(0.0);
		}
		// Loads transactions File into transactionList Vector
			for(int x = 0;x<=allTransactionsList.size()-1; x++) {
						if(allTransactionsList.get(x).getTransactionType().equals("Buy")&&(CustomerID == allTransactionsList.get(x).getCustomerID())) {
							addShares(allTransactionsList.get(x).getStockSymbol(), allTransactionsList.get(x).getQuantity());
						}
						if(allTransactionsList.get(x).getTransactionType().equals("Sell")&&(CustomerID == allTransactionsList.get(x).getCustomerID())) {
							subtractShares(allTransactionsList.get(x).getStockSymbol(), allTransactionsList.get(x).getQuantity());
						}
					}			
					
	}
	
	
	
	/**
	 * Auxiliary method to updatePortfolio().  Simply processes the effect of a buy transaction to the account portfolio, buy adding to the numberOfShares vector of
	 * a given stock.
	 * @param stockSymbol
	 * @param shares
	 */
	public void addShares(String stockSymbol, double shares) {
		//Sets the number of shares of the designated stock
		int index = -1;
		int x = 0;
		while(index == -1){
			if(stocksList.get(x).getStockSymbol().equals(stockSymbol)) {
				index = x;	
			}
			x++;	
		}
		System.out.println("Just added " + shares + " to " + stocksList.get(index).getStockSymbol() + ", which already had " + numberOfShares.get(index)); 
		numberOfShares.set(index, (numberOfShares.get(index) + shares));
		
	}
	
	/**
	 * Auxiliary method to updatePortfolio().  Simply processes the effect of a sell transaction to the account portfolio, by subtracting from the numberOfShares vector of
	 * a given stock.
	 * @param stockSymbol
	 * @param shares
	 */
	public void subtractShares(String stockSymbol, double shares) {
		//Sets the number of shares of the designated stock
		int index = -1;
		int x = 0;
		while(index == -1){
			if(stocksList.get(x).getStockSymbol().equals(stockSymbol) ) {
				index = x;	
			}
			x++;	
		}
		numberOfShares.set(index, (numberOfShares.get(index) - shares));
	}
	
	
	
	


	

	
	/**
	 * Returns the number of shares of a given stock in the given account up through a single day.  
	 * @param stockSymbol
	 * @param day
	 */
	public void getSharesUpToDate(int day){
		if(sharesThroughDate.size() > 0){
		sharesThroughDate.clear();
		}
		for(int x = 0; x<=stocksList.size()-1; x++){
			sharesThroughDate.add(0.0);
		}
		
		// Loads transactions File into transactionList Vector
		for(int x = 0;x<=allTransactionsList.size()-1; x++) {
					if(allTransactionsList.get(x).getTransactionType().equals("Buy")&&(CustomerID == allTransactionsList.get(x).getCustomerID())&&(allTransactionsList.get(x).getTransactionDate() <= day)) {
						addSharesTillDate(allTransactionsList.get(x).getStockSymbol(), allTransactionsList.get(x).getQuantity());
					}
					if(allTransactionsList.get(x).getTransactionType().equals("Sell")&&(CustomerID == allTransactionsList.get(x).getCustomerID())&&(allTransactionsList.get(x).getTransactionDate() <= day)) {
						subtractSharesTillDate(allTransactionsList.get(x).getStockSymbol(), allTransactionsList.get(x).getQuantity());
					}
				}
	}
	
	/**
	 * Auxiliary method to getSharesUpToDate().  Adds a certain quantity of shares to the vector that keeps track of how many shares of each stock there is
	 * up through a given date.  
	 * @param stockSymbol
	 * @param shares
	 */
	public void addSharesTillDate(String stockSymbol, double shares) {
		//Sets the number of shares of the designated stock
		int index = -1;
		int x = 0;
		while(index == -1){
			if(stocksList.get(x).getStockSymbol().equals(stockSymbol)) {
				index = x;	
			}
			x++;	
		}
		System.out.println("Just added " + shares + " to " + stocksList.get(index).getStockSymbol() + ", which already had " + sharesThroughDate.get(index)); 
		sharesThroughDate.set(index, (sharesThroughDate.get(index) + shares));
		
	}
	
	
	/**
	 * Auxiliary method to getSharesUpToDate().  Subtracts a certain quantity of shares from the vector that keeps track of how many shares of each stock there is
	 * up through a given date.  
	 * @param stockSymbol
	 * @param shares
	 */
	public void subtractSharesTillDate(String stockSymbol, double shares) {
		//Sets the number of shares of the designated stock
		int index = -1;
		int x = 0;
		while(index == -1){
			if(stocksList.get(x).getStockSymbol().equals(stockSymbol) ) {
				index = x;	
			}
			x++;	
		}
		sharesThroughDate.set(index, (sharesThroughDate.get(index) - shares));
	}
	
	
	
	
	/**
	 * Getters and setters
	 * @return
	 */
	
	public Vector<Double> getNumberOfShares() {
		return numberOfShares;
	}
	
	public Vector<Double> getSharesThroughDate() {
		return sharesThroughDate;
	}
	
	
	public Vector<Transaction> getAllTransactionsList() {
		return allTransactionsList;
	}



	public void setAllTransactionsList(Vector<Transaction> allTransactionsList) {
		this.allTransactionsList = allTransactionsList;
	}
	
	public int getCustomerID() {
		return CustomerID;
	}



	public void setCustomerID(int customerID) {
		CustomerID = customerID;
	}




}
